const appInstance = getApp()
Component({
  data: {
    originFiles: [
      {
        url: 'https://tdesign.gtimg.com/mobile/demos/example4.png',
        name: 'uploaded1.png',
        type: 'image',
      },
      {
        url: 'https://tdesign.gtimg.com/mobile/demos/example6.png',
        name: 'uploaded2.png',
        type: 'image',
      },
      {
        url: 'https://tdesign.gtimg.com/mobile/demos/example5.png',
        name: 'uploaded3.png',
        type: 'image',
      },
    ],
    gridConfig: {
      column: 4,
      width: 160,
      height: 160,
    },
    config: {
      count: 1,
    },
    image: null,
    user_name:'',
    user_status:'',
    disabled:true,
    visible:false,
    verify_loading_visible:false,
    send_result:false,
    send_disabled:false
  },
  methods:{
    handleSuccess(e) {
      const { files } = e.detail;
      this.setData({
        originFiles: files,
      });
    },
    handleRemove(e) {
      const { index } = e.detail;
      const { originFiles } = this.data;
      originFiles.splice(index, 1);
      this.setData({
        originFiles,
      });
    },
    handleClick(e) {
      console.log(e.detail.file);
    },
  
    displayVisible(){
      this.setData({visible:true})
    },
    checkFieldRequired(){
      let visitor_phone = this.data.visitor_phone
      let phone_number_code = this.data.phone_number_code
      let wecom_userid = this.data.wecom_userid
      let respondent_name = this.data.respondent_name
      if (!visitor_phone){
        wx.showToast({
          title: '请填写手机号',
          icon: 'error',
          duration: 2000
        })
      }else if(!phone_number_code){
        wx.showToast({
          title: '请填验证码',
          icon: 'error',
          duration: 2000
        })
      }else if (!wecom_userid){
        wx.showToast({
          title: '请填写员工号',
          icon: 'error',
          duration: 2000
        })
      } else if(!respondent_name){
        wx.showToast({
          title: '请填写姓名',
          icon: 'error',
          duration: 2000
        })
      }
      else{
        this.regisiter_respondent()
      }
    },
    get_user_name(e){
      this.setData({respondent_name:e.detail.value})
    },
    regisiter_respondent(e){
      this.setData({verify_loading_visible:true, text:'注册中,稍安勿躁...', msg:null})
      let that = this
      wx.login({
        success: (res) => {
          wx.request({
            url: appInstance.globalData.request_url + "factory/create_user_and_login",
            method:"POST",
            data:{
              "wechat_user_code":res.code,
              "user_phone":that.data.visitor_phone,
              "user_phone_code":that.data.phone_number_code,
              "wecom_userid":that.data.wecom_userid,
              "user_group":"Respondent",
              "respondent_name":that.data.respondent_name,
            },
            dataType:"json",
            success (resp) {
              console.log(resp)
              appInstance.globalData.cookies = resp.cookies[0]
              if(resp.statusCode==200){
                if(resp.data.result.status == 200){
                  appInstance.globalData.cookies = resp.cookies[0]
                  appInstance.globalData.user_status = 'has_login'
                  appInstance.globalData.user_name = resp.data.result.visitor_name
                  that.setData({verify_loading_visible:false, visible:false,text:'注册成功'})
                  wx.showToast({
                    title: '注册成功',
                    icon: 'success',
                    duration: 2000
                  })
                  that.onShow()
                }else{
                  that.setData({msg:resp.data.result.msg,verify_loading_visible:false})
                }
              }
            },
            fail (err){
              that.closeOverlay()
              that.setData({msg:err})
              console.log('失败')
              wx.showToast({
                title: resp.data,
                icon: 'error',
                duration: 2000
              })
            }
          })
        },
        fail(err){
          that.closeOverlay()
          console.log(err)
        }
      })
    },
  
    send_phone_verify_code(e){
      this.setData({send_disabled:true, verify_loading_visible:true, text:'发送中,验证码有效期5分钟..',display_verify_code_input:false})
      let that = this
      wx.login({
        success: (res) => {
          this.setData({send_disabled:false})
          wx.request({
            url: appInstance.globalData.request_url + "factory/send_phone_verify_code",
            method:"POST",
            data:{
              "wx_login_code":res.code,
              "phone_number":that.data.visitor_phone,
            },
            dataType:"json",
            success (resp) {
              console.log(resp)
              that.setData({verify_loading_visible:false})
              if(resp.statusCode==200){
                if(resp.data.result.status_code==200){
                  that.setData({send_result:true, msg:resp.data.result.msg,sms_log_id:resp.data.result.sms_log_id})
                }else{
                  that.setData({send_result:false,msg:resp.data.result.msg})
                }
              }else{
                wx.showToast({
                  title: '服务器错误',
                  icon: 'error',
                  duration: 2000
                })
              }
            },
            fail (err){
              console.log('失败')
              that.setData({verify_loading_visible:false})
              wx.showToast({
                title: err,
                icon: 'error',
                duration: 2000
              })
            }
          })
        },
        fail(err){
          this.setData({send_disabled:false,verify_loading_visible:false})
          console.log(err)
        }
      })
    },
    cancel_visitor_coding_submit(e){
      this.setData({visible:false,send_result:false,msg:null})
    },
    get_phone_number(e){
      this.setData({visitor_phone:e.detail.value})
    },
    get_phone_number_code(e){
      this.setData({phone_number_code:e.detail.value})
    },
    get_wecom_userid(e){
      this.setData({wecom_userid:e.detail.value})
    },
    login_out(e){
      console.log(111111111)
      appInstance.globalData.cookies = null
      appInstance.globalData.user_status = "no_login"
      this.onShow()

    },
    bind_name_value:function (e) {
      this.setData({user_name:e.detail.value})
    },
    return_index:function (params) {
      wx.switchTab({
        url: "/pages/visitor_coding/index",
        fail(err){
          console.log(err)
        }
      })
    },
    return_visitor_submit_list:function (params) {
      wx.switchTab({
        url: "/pages/visitor_submit_list/index",
        fail(err){
          console.log(err)
        }
      })
    },
    return_customer_service_web:function (params) {
      const src_url = "https://mp.weixin.qq.com/wxawap/wapreportwxadevlog?action=complain_feedback&appid=wx641768494ae2520e&from=3#wechat_redirect"
      wx.navigateTo({
        url: "/pages/form/index",
        success: function(res) {
          // 通过eventChannel向被打开页面传送数据
          console.log('传输的数据是' + src_url)
          res.eventChannel.emit('acceptDataFromOpenerPage', { src_url: src_url })
        }
      })
    },
    onShow(){
      appInstance.setLoginData(this)
    },
    onChooseAvatar(e) {
      console.log(e)
      const avatarUrl = e.detail .avatarUrl
      this.setData({
        image:avatarUrl,
        disabled:false
      })
    },
    onHide(){
      this.setData({
        disabled:true
      })
    }
  }
});